package com.rongshu.view.login.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.login.contract.AuthCodeLoginView;
import com.rongshu.view.login.entity.PasswordLogin;
import com.rongshu.view.login.entity.ThirdPartyClass;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

public class AuthCodeLoginPresenter extends BasePresenter<AuthCodeLoginView> {

    public AuthCodeLoginPresenter(Context context) {
        super(context);
    }

    /**
     * 第三方账户登陆
     */
    public void thirdPartyLogin(int type, String registrationid, String open_id, String nickname, String HeadPortraits, String phone) {
        ((BaseActivity)mContext).showLoading(true);
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("registrationid", registrationid);
        //ThirdPartyClass
        ThirdPartyClass thirdPartyClass = new ThirdPartyClass();
        thirdPartyClass.setOpen_id(open_id);
        thirdPartyClass.setNickname(nickname);
        thirdPartyClass.setAvatar(HeadPortraits);
        thirdPartyClass.setMobile(phone);
        RetorfitService.postQQWechatWeiBoLog(type, registrationid, Common.encryptionSign(map), thirdPartyClass)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<PasswordLogin>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ((BaseActivity)mContext).showLoading(false);
                    }

                    @Override
                    public void onNext(PasswordLogin passwordLogin) {
                        ((BaseActivity)mContext).showLoading(false);
                        getView().getUserRegisterforMobileCode(passwordLogin);
                    }
                });
    }
}
